
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.UserBean;
import validation.*;
import database.DBClass;
import java.util.List;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Jagadeesh
 */
public class ControllerServlet extends HttpServlet {

    //declare values to get form values from jsp page
    String fullName, idGudangAdmin;
    String userName;
    String password;
    String dateOfBirth;
    String email;
    String phoneNo;
    String idSenjata, idSen;
    String namaSenjata;
    String jenis;
    String thnDibuat;
    String thnDibeli;
    String idGudang;
    String status, idStatus, namaStatus;
    String action;
    String provinsi, kabupaten, kecamatan, desa;
    UserBean bean = new UserBean();
    ValidateForm validateform = new ValidateForm();
    //  ValidateLogin validatelogin = new ValidateLogin();
    DBClass dbobject = new DBClass();

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            //get the values from jsp page
            fullName = request.getParameter("fullName");
            userName = request.getParameter("userName");
            password = request.getParameter("password");
            dateOfBirth = request.getParameter("dateOfBirth");
            email = request.getParameter("email");
            phoneNo = request.getParameter("phoneNo");
            provinsi = request.getParameter("provinsi");
            kabupaten = request.getParameter("kabupaten");
            kecamatan = request.getParameter("kecamatan");
            desa = request.getParameter("desa");
            idSenjata = request.getParameter("idSenjata");
            namaSenjata = request.getParameter("namaSenjata");
            jenis = request.getParameter("jenis");
            idGudang = request.getParameter("idGudang");
            thnDibuat = request.getParameter("thnDibuat");
            thnDibeli = request.getParameter("thnDibeli");
            status = request.getParameter("status");
            action = request.getParameter("action");

            if (action.equals("submit3")) {
                //set values to bean.For this call below method
                setValuesToBean();

                //check all form values are valid or not. send bean object 
                UserBean checkedbean = validateform.validateData(bean);
                if (!checkedbean.getIsValid()) {

                    //if data is invalid.set bean object in request and pass that request to
                    //insertupdate.jsp using forward
                    checkedbean.setAction("submit");
                    request.setAttribute("error", checkedbean);

                    RequestDispatcher rd = request.getRequestDispatcher("tambahRekap.jsp");
                    rd.forward(request, response);
                    //now display errors in that jsp page
                } else {
                    //using DBClass object call insertDetails method and pass bean object
                    dbobject.insertDetails2(bean);
                    List list = dbobject.getAlldetails2();
                    request.setAttribute("list", list);
                    //forward to insertupdate page using requestdispatcher
                    RequestDispatcher rd = request.getRequestDispatcher("viewRekap1.jsp");
                    //display a message to client.store message in request object
                    //forwarding to jsp
                    rd.forward(request, response);

                }
            }
            if (action.equals("submit")) {
                //set values to bean.For this call below method
                System.out.println(status);
                setValuesToBean();
                System.out.println("jenis" + bean.getJenis());
                System.out.println("gudang" + bean.getIdGudang());
                System.out.println("status" + bean.getStatus());
                if (!bean.equals(null)) {
                    dbobject.insertDetails2(bean);
                    List list = dbobject.getAlldetails2();
                    request.setAttribute("list", list);
                    //forward to insertupdate page using requestdispatcher
                    RequestDispatcher rd = request.getRequestDispatcher("viewRekap1.jsp");
                    //display a message to client.store message in request object
                    //forwarding to jsp
                    rd.forward(request, response);
                   

                }







            }
//            if (action.equals("login")) {
//
//               // setValuesLogin();
//                dbobject.getPass(userName);
//              //  UserBean checkedbean = validatelogin.validateLogin(bean);
//                if(userName.equals(bean.getPassword())){
//                 RequestDispatcher rd = request.getRequestDispatcher("view.jsp");
//                   rd.forward(request, response);
//            }
//                
//            }
            if (action.equals("cancel")) {
                dbobject.UpateDetails(bean, userName);
                List list = dbobject.getAlldetails();
                request.setAttribute("list", list);
                //forward to insertupdate page using requestdispatcher
                RequestDispatcher rd = request.getRequestDispatcher("view.jsp");
                //display a message to client.store message in request object
                //forwarding to jsp
                rd.forward(request, response);
            }
            if (action.equals("cancel2")) {
                dbobject.UpdateDetails2(bean, idSenjata);
                List list = dbobject.getAlldetails2();
                request.setAttribute("list", list);
                //forward to insertupdate page using requestdispatcher
                RequestDispatcher rd = request.getRequestDispatcher("viewRekap1.jsp");
                //display a message to client.store message in request object
                //forwarding to jsp
                rd.forward(request, response);
            }

            if (action.equals("edit2")) {
                //get userdetails of particular name

                UserBean ubean = dbobject.getDetails2(idSenjata);

                request.setAttribute("updateuser", ubean);
                RequestDispatcher rd = request.getRequestDispatcher("tambahRekap1.jsp");

                rd.forward(request, response);

            }
            if (action.equals("ubah")) {
                setValuesToBean();
                idSenjata = request.getParameter("idSenjata");
                System.out.println("idsenjata" + idSenjata);

                //using DBClass object call insertDetails method and pass bean object
                dbobject.UpdateDetails2(bean, idSenjata);
                List list = dbobject.getAlldetails2();
                request.setAttribute("list", list);
                //forward to insertupdate page using requestdispatcher
                RequestDispatcher rd = request.getRequestDispatcher("viewRekap1.jsp");
                //display a message to client.store message in request object
                //forwarding to jsp
                rd.forward(request, response);

            }


            if (action.equals("delete")) {
                //delete userdetails of particular name
                dbobject.deleteDetails(userName);
                List list = dbobject.getAlldetails();
                request.setAttribute("list", list);
                RequestDispatcher rd = request.getRequestDispatcher("view.jsp");
                rd.forward(request, response);
            }
            if (action.equals("delete2")) {
                //delete userdetails of particular name
                HttpSession session = request.getSession();
                if (session.getAttribute("userName") == null) {

// <<<<<<< .mine --> kalau ditambahin ini, dipunyaq error tholib
                    RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
                    rd.forward(request, response);
                } else {
                    dbobject.deleteDetails2(idSenjata);
                    List list = dbobject.getAlldetails2();
                    request.setAttribute("list", list);
                    RequestDispatcher rd = request.getRequestDispatcher("viewRekap.jsp");
                    rd.forward(request, response);
                }
//======= ini juga bikin error
                dbobject.deleteDetails2(idSenjata);
                List list = dbobject.getAlldetails2();
                request.setAttribute("list", list);
                RequestDispatcher rd = request.getRequestDispatcher("viewRekap1.jsp");
                rd.forward(request, response);
//>>>>>>> .r12 ini juga bikin error
            }

            if (action.equals("inventori")) {
                dbobject.UpateDetails(bean, userName);
                List list = dbobject.getAlldetails();
                request.setAttribute("list", list);
                //forward to insertupdate page using requestdispatcher
                RequestDispatcher rd = request.getRequestDispatcher("view.jsp");
                //display a message to client.store message in request object
                //forwarding to jsp
                rd.forward(request, response);
            }
            if (action.equals("rekap")) {
//                dbobject.UpdateDetails2(bean, idSenjata);
               HttpSession session = request.getSession();
                if (session.getAttribute("userName") == null) {

                    RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
                    rd.forward(request, response);
                } else {
                                   
                String userName = session.getAttribute("user").toString();
                System.out.println("user" + userName);
//                LoginServlet haha = new LoginServlet();


                List list1 = dbobject.getAlldetailsok(bean, userName);
                System.out.println("idGudangAdmin " + idGudangAdmin);
                request.setAttribute("list", list1);
                //forward to insertupdate page using requestdispatcher
                RequestDispatcher rd = request.getRequestDispatcher("viewRekap1.jsp");
                //display a message to client.store message in request object
                //forwarding to jsp
                rd.forward(request, response);
            }}
            if (action.equals("logout")) {
//                dbobject.UpdateDetails2(bean, idSenjata);
                HttpSession session = request.getSession();

//                session.equals(out);
                session.invalidate();


                //forward to insertupdate page using requestdispatcher
                RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
                //display a message to client.store message in request object
                //forwarding to jsp
                rd.forward(request, response);
            }
            if (action.equals("tambah")) {

                //forward to insertupdate page using requestdispatcher
                RequestDispatcher rd = request.getRequestDispatcher("tambahRekap1.jsp");
                //display a message to client.store message in request object
                //forwarding to jsp
                rd.forward(request, response);
            }

        } catch (Exception e) {
            out.println(e);
        } finally {
            out.close();
        }
    }

    //this method is used to setvalues to bean
    public void setValuesToBean() {
        bean.setfullName(fullName);
        bean.setUserName(userName);
        bean.setPassword(password);
        bean.setDateOfBirth(dateOfBirth);
        bean.setEmail(email);
        bean.setPhoneNo(phoneNo);
        bean.setProvinsi(provinsi);
        bean.setKabupaten(kabupaten);
        bean.setKecamatan(kecamatan);
        bean.setDesa(desa);
        bean.setIdSenjata(idSenjata);
        bean.setNamaSenjata(namaSenjata);
        bean.setJenis(jenis);
        bean.setIdGudang(idGudang);
        bean.setThnDibuat(thnDibuat);
        bean.setThnDibeli(thnDibeli);
        bean.setStatus(status);

    }

    public void setValuesLogin() {
        bean.setUserName(userName);
        bean.setPassword(password);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

//    protected void doPost(HttpServletRequest request, HttpServletResponse response)
//            throws ServletException, IOException {
//        processRequest(request, response);
//    }

    public String getServletInfo() {
        return "Short description";
    }
}
